import Vue from 'vue'
import VueRouter from 'vue-router'


Vue.use(VueRouter)

//解决vue路由重复导航错误
//获取原型对象上的push函数
const originalPush = VueRouter.prototype.push
//修改原型对象中的push方法
VueRouter.prototype.push = function push(location) {
    return originalPush.call(this, location).catch(err => err)
}


const routes = [
    {
        path: '/', component: () => import('@/components/layout/index'), redirect: '/home',
        children: [
            {
                path: 'home',
                name: 'Home',
                component: () => import('@/views/home/index')
            },
            {
                name: 'PageNull',
                path: '404',
                component: () => import('@/views/404')
            },
            {
                name: 'Auther',
                path: 'auther',
                component: () => import('@/views/auther/index')
            },
            {
                name: 'Goods',
                path: 'goods',
                component: () => import('@/views/goods/index')
            }
        ]
    }
]


const router = new VueRouter({
    routes
})

router.beforeEach((to, from, next) => {

    if (to.matched.length === 0) {
       next({name: 'PageNull'})
    } else {
        next()
    }

})

export default router
